package com.sjzl.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sjzl.entity.Board;
import com.sjzl.entity.Post;
import com.sjzl.entity.User;
import com.sjzl.service.BoardService;
import com.sjzl.service.PostService;
import com.sjzl.service.UserService;

@Component("postServiceImpl")
public class PostServiceImpl extends ServiceImpl<Post> implements PostService{
	
	@Resource(name="boardServiceImpl")
	private BoardService boardService;
	
	@Resource(name="userServiceImpl")
	private UserService userService;
	
	@Override
	public void create(Post post){}

	@Transactional
	public void create(Post post, Board board, User user) {
		post.setBoard(board);
		post.setUser(user);
		post.setReplyCount(0);
		post.setSelectCount(0);
		post.setReportCount(0);
		post.setDateCreated(new Date());
		this.getDao().save(post);
		
		User dbUser = userService.get(User.class, user.getId());
		dbUser.setPostCount(dbUser.getPostCount()+1);
		userService.update(dbUser);
		
		Board parentBoard;
		int parentId;
		if(board.getSubBoard().size() == 0){
			parentId = board.getBoard().getId();
			parentBoard = boardService.get(Board.class, parentId);
			parentBoard.setLastPost(post);
			parentBoard.setPostCount(parentBoard.getPostCount()+1);
			boardService.update(parentBoard);
		}else{
			parentId = board.getId();
			parentBoard = boardService.get(Board.class, parentId);
			parentBoard.setLastPost(post);
			boardService.update(parentBoard);
		}
	}

	public List<Post> getPostListByBoardId(Integer board_id) {
		String hql = "from Post p where p.board = ";
		List<Post> postList = this.getDao().list(hql + board_id);
		Board board = boardService.get(Board.class, board_id);
		if(postList.size() == 0){
			List<Board> boardList = board.getSubBoard();
			for(Board b : boardList){
				int id = b.getId();
				List<Post> list = this.getDao().list(hql + id);
				postList.addAll(list);
			}
		}
		return postList;
	}
	
	public List<Post> getPagePostListByBoardId(Integer board_id, int pageNo, int pageSize) throws Exception{
		int firstResult = (pageNo - 1) * pageSize;
		String hql = "from Post p where p.board = ";
		List<Post> postList = this.getDao().list(hql + board_id + " order by p.dateCreated desc", firstResult, pageSize);
		Board board = boardService.get(Board.class, board_id);
		if(postList.size() == 0){
			List<Board> boardList = board.getSubBoard();
			for(Board b : boardList){
				int id = b.getId();
				List<Post> list = this.getDao().list(hql + id);
				postList.addAll(list);
			}
			if(pageNo == postList.size()/pageSize && postList.size()%pageSize == 0){
				postList = postList.subList(firstResult,postList.size());
			}else if(pageNo == (postList.size()/pageSize +1)&& postList.size()%pageSize != 0){
				postList = postList.subList(firstResult,postList.size());
			}else{
				postList = postList.subList(firstResult,firstResult+pageSize);
			}
		}
		return postList;
	}

	public Integer getTotalCount(String entityName, String where, Integer board_id) {
		int totalCount = this.getDao().getTotalCount("Post", where + board_id);
		if(totalCount == 0){
			Board board = boardService.get(Board.class, board_id);
			List<Board> boardList = board.getSubBoard();
			for(Board b : boardList){
				int id = b.getId();
				int sub = this.getDao().getTotalCount("Post", where + id);
				totalCount += sub;
			}
		}
		return totalCount;
	}

	@Transactional
	public boolean deletePost(Integer post_id) throws Exception {
		Post post = this.getDao().get(Post.class, post_id);
		Board pBoard = post.getBoard().getBoard();
		Board board = post.getBoard();
		
		List<User> pModerators = pBoard.getModerators();
		List<User> moderators = board.getModerators();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession httpSession = request.getSession();
		User sessionUser = (User) httpSession.getAttribute("sessionUser");
		
		for(User u : pModerators){
			if(sessionUser.getAccount().equals(u.getAccount())){
				this.delete(post);
				return true;
			}
		}
		for(User u : moderators){
			if(sessionUser.getAccount().equals(u.getAccount())){
				this.delete(post);
				return false;
			}
		}
		
		return false;
		
	}

	public Post getLast(Integer board_id) {
		String hql = "from Post where id = (select max(id) from Post)";
		List<Post> postList = this.getDao().list(hql);
		Post post =postList.get(0);
		return post;
	}

	@Transactional
	public boolean report(Integer post_id) {
		Post dbPost = this.getDao().get(Post.class, post_id);
		dbPost.setReportCount(dbPost.getReportCount()+1);
		this.getDao().update(dbPost);
		return true;
	}
	
}
